package com.guli.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonUtils.JwtUtils;
import com.guli.commonUtils.R;
import com.guli.commonUtils.vo.UcenterVO;
import com.guli.eduService.client.UcenterClient;
import com.guli.eduService.entity.EduComment;
import com.guli.eduService.service.impl.EduCommentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-25
 */
@Api(description = "评论管理模块")
@RestController
@RequestMapping("/edu/comment")
//@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentServiceImpl commentService;

    @Autowired
    private UcenterClient ucenterClient;

    // TODO 前端请求发送到nginx（localhost:9001），未能够将其转发到当前微服务模块service-comments（调用方法）;
    @ApiOperation(value = "根据课程id，分页查询当前课程下的所有评论")
    @GetMapping("/getComments/{currentPage}/{limit}")
    public R getCommentsByCourseId(
            @ApiParam(name = "currentPage" , value = "当前页码" , required = true)
            @PathVariable Long currentPage ,

            @ApiParam(name = "limit" , value = "限制记录数" , required = true)
            @PathVariable Long limit ,

            String courseId){
        // 1.获取分页插件对象
        Page<EduComment> page = new Page<>(currentPage , limit);
        // 2.将所查询到符合条件的数据，添加到page对象中;
        commentService.getCommentByCourseId(page , courseId);
        // 3，从page对象中获取数据
        List<EduComment> commentList = page.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("records", commentList);
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("/addComment/save")
    public R addComment(
            @ApiParam(name = "comment" , value = "评论基本信息" , required = true)
            @RequestBody EduComment comment ,

            HttpServletRequest request){
        // 根据request的请求头中所存储到token字符串，获取到会员id信息;
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // 该用户还未登录，不能够进行评论;
        if(StringUtils.isEmpty(memberId)){
            return R.error().message("请先进行登录！！！");
        }
        comment.setMemberId(memberId);
        // TODO 实现调用微服务service-ucenter中方法，获取到当前会员id所对应的基本信息
        UcenterVO memberInfo = ucenterClient.getMemberInfoByMemberId(memberId);
        comment.setNickname(memberInfo.getNickname());
        comment.setAvatar(memberInfo.getAvatar());

        boolean ifSave = commentService.save(comment);
        if(ifSave){
            return R.ok().message("添加该评论成功~~~");
        }
        return R.error().message("添加该评论失败！！！");
    }
}

